package com.easy.controller;

import com.easy.bean.Member;
import com.easy.service.LoginService;
import com.easy.util.CaptchaUtil;
import com.easy.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/login")
@CrossOrigin
public class LoginController {
    @Autowired
    LoginService loginService;

    @GetMapping("/captcha")
    public Result captcha(){
        return Result.success(CaptchaUtil.getCaptcha());
    }
    @PostMapping("/dologin")
    public Result login(Member member,@RequestParam("captcha") String captcha,@RequestParam("key")String key){
        //验证码验证
        if(!CaptchaUtil.checkCaptcha(key,captcha)){
            return Result.error(500,"验证码错误");
        }
        //登录
        try {
            String token = loginService.login(member);
            //删除掉验证码
            return Result.success("登陆成功",token);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error(500,e.getMessage());
        }
    }
}
